let validationRules = {
  isEmpty(value, errorMsg) {
    return value === '' ? errorMsg : void 0
  },
  isIdCard(value, errorMsg) {
    return !/(^1[0-9]{10}$)/.test(value) ? errorMsg : void 0
  },
  isMoblie(value, errorMsg) {
    return !/^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value) ? errorMsg : void 0
  },
  isPersonalId(value, errorMsg) {
    return /^[0-9]{2}\d{8}$/.test(value) ? errorMsg : void 0
  },
  isEmail(value, errorMsg) {
    return !/^\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value) ? errorMsg : void 0
  },
  isIdCard(value, errorMsg) {
    return !/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value) ? errorMsg : void 0
  },
  isVerification(value, errorMsg) {
    return !/^\d{6}$/.test(value) ? errorMsg : void 0
  }
}


class Validator {
  constructor(_el) {
    this.cache = [], //保存校验规则
      this.el = _el
  }
  add(lable, rules) {
    for (let rule of rules) {
      let strategyAry = rule.strategy.split(':')
      let errorMsg = rule.errorMsg
      this.cache.push(() => {
        let strategy = strategyAry.shift()
        strategyAry.unshift(this.el[lable])
        strategyAry.push(errorMsg)
        return validationRules[strategy].apply(this.el, strategyAry)
      })
    }
  }
  start() {
    for (let validatorFunc of this.cache) {
      let errorMsg = validatorFunc()
      if (errorMsg) {
        return errorMsg
      }
    }
  }
}
export default Validator